﻿var isPreviousStageLoaded = false;
var editContainer = $('#editcontainer');
var menuContainer = $('#editmenucontainer');
var orderId = $('#Id').val();

var LoadSpecificStage = function (stageid, stage) {
    isPreviousStageLoaded = true;
    PostReturningHtml("/order/specificstage/", { StageId: stageid, Stage: stage }, editContainer, function () {
        $('#cancel').show();
        $('#navcancelli').show();
        $('#save').text('Save');
        RegisterContentEvents();
        LoadMenu(stageid);
    });
};
    
var RegisterMenuEvents = function () {
    $('.jumpstage').click(function (event) {
        event.preventDefault();
        LoadSpecificStage($(this).attr('stageid'), $(this).attr('stage'));
    });

    $('.returnToCurrent').click(function (event) {
        event.preventDefault();
        LoadDefaultContent();
    });

    $('#attachments').click(function () {
        GetHtml("/attachment/attachments/" + $('#Id').val(), editContainer, function () {
            $('#filestab a:first').tab('show');

            $('.tabclickable').click(function (e) {
                e.preventDefault();
                $(this).tab('show');
            });

            $('.tab-pane:first').addClass('active');

            $('.carousel').carousel({
                interval: 5000
            });
        });
    });
};

var LoadMenu = function (stageId) {
    PostReturningHtmlNoAnnimation("/order/menu", { OrderId: $('#Id').val(), ActiveStageId: stageId }, menuContainer, function() {
        $.each($('#dropdownmenuitems').children(), function() {
            $(this).appendTo('#editdropmenucontainer');
        });
        $('#dropdownmenuitems').remove();
        RegisterMenuEvents();
    });
};

var SaveStage = function () {
    PostReturningHtml($('#editform').attr('url'), $('#editform').serialize(), editContainer, function () {
        RegisterContentEvents();
        LoadMenu();
    });
};

var RegisterContentEvents = function () {

    $('#save').click(function (event) {
        event.preventDefault();
        SaveStage();
    });

    $('#cancel').click(function (event) {
        event.preventDefault();
        LoadDefaultContent();
    });

    $('.intspinner').spinner({
        min: 0,
        max: 200,
        spin: function () {
            $(this).change();
        }
    });

    RegisterDatePicker('.datecontrol');

    $('#addcomponentbtn').click(function () {
        GetHtml("/order/addcomponent/" + orderId, editContainer, function () {
            RegisterContentEvents();
        });
    });
};

var LoadDefaultContent = function () {
    isPreviousStageLoaded = false;
    GetHtml("/order/currentstage/" + orderId, editContainer, function () {
        RegisterContentEvents();
        $('#cancel').hide();
        $('#navcancelli').hide();
        LoadMenu();
    });
};

var RegisterEvents = function () {

    $('#navsave').click(function () {
        SaveStage();
    });

    $('#navcancel').click(function () {
        LoadDefaultContent();
    });

    $('#addcomponent').click(function () {
        GetHtml("/order/addcomponent/" + orderId, editContainer, function () {
            RegisterContentEvents();
        });
    });
};

$(document).ready(function() {
    RegisterEvents();
    LoadDefaultContent();
});